<template>
<div id="container"></div>
</template>

<script>
import AMapLoader from '@amap/amap-jsapi-loader'
window._AMapSecurityConfig = {
  securityJsCode: '683b4d8f04c4254dd4ab4bc1aaa0d039'
};
export default {
  data() {
    return {
      map: null,
    }
  },
  methods: {
    initMap() {
      AMapLoader.load({
        key: '3f8a09fca06a15423c4d18fc3f47dfee', // 申请好的Web端开发者Key，首次调用 load 时必填
        version: '2.0', // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins: ['AMap.ToolBar', 
        'AMap.Scale', 'AMap.HawkEye', 
        'AMap.MapType', 'AMap.Geolocation', 
        'AMap.AutoComplete', 
        'AMap.PlaceSearch',
        'AMap.DistrictSearch'
        ] // 需要使用的的插件列表，如比例尺'AMap.Scale'等
      }).then(AMap => {
          this.map = new AMap.Map('container', {
            //设置地图容器id
            viewMode: '3D', //是否为3D地图模式
            zoom: 10, //初始化地图级别
            center: [113.357224,34.977186] //初始化地图中心点位置
          })
           let positionArr = [
                    [113.357224,34.977186],
                    [114.555528,37.727903],
                    [112.106257,36.962733],
                    [109.830097,31.859027],
                    [116.449181,39.98614],
                ];
                // 创建 AMap.Icon 实例：
            var icon = new AMap.Icon({
                size: new AMap.Size(40, 50),    // 图标尺寸
                image: 'https://files.catbox.moe/llpdno.png',  // Icon的图像
                imageOffset: new AMap.Pixel(0, -60),  // 图像相对展示区域的偏移量，适于雪碧图等
                imageSize: new AMap.Size(40, 50)   // 根据所设置的大小拉伸或压缩图片
            });
                for(let item of positionArr){
                    let marker = new AMap.Marker({
                        position:[item[0],item[1]],
                        icon:icon
                    });
                    this.map.add(marker);
                }
          this.map.addControl(new AMap.Scale())
          this.map.addControl(new AMap.ToolBar())
          this.map.addControl(new AMap.HawkEye())
          this.map.addControl(new AMap.MapType())
          this.map.addControl(new AMap.Geolocation())
          this.auto = new AMap.AutoComplete(this.autoOptions)
          this.placeSearch = new AMap.PlaceSearch({
            map: this.map
          }) //构造地点查询类
          this.auto.on('select', this.select)
        })
        .catch(e => {
          console.log(e)
        })
    },
  },
  mounted() {
    //DOM初始化完成进行地图初始化
    // this.$nextTicket(() => {
    //   this.initMap()
    // })
    // this.initMap()
    // window.onLoad = function() {
      this.initMap()
    // }
  },
  destroyed() {
    this.map.destroy()
  },
  created() {
  },
  watch: {
    searchPlaceInput(newValue) {
      if (newValue != null) {
        console.log(newValue)
        this.placeSearch.search(newValue)
        this.drawBounds(newValue)
        this.map.setZoom(16, true, 1)
      }
    },
    showHeatOrNot(newValue) {
      if (newValue) {
        this.showHeatMap()
      } else {
        this.heatmap.hide()
      }
    }
  }
}
</script>
<style lang="less" scoped>
#container {
  padding: 0px;
  margin: 0px;
  width: 400px;
  height: 400px;
}
</style>